﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace RefOffDesktop
{
    /*
     * Class: RefereeProfile
     * 
     * Use: Form used by the referee to enter their personal details,
     *      which are used when filling in misconduct reports
     *      
     * Author: Mike Skaife, mcs3@aber.ac.uk
     */ 
    public partial class RefereeProfile : Form
    {
        //The main RefOff screen object from where
        //this form was opened
        private RefOff referrer;

        //Details required to connect to database
        SqlCeConnection conn;
        String connection;

        public RefereeProfile(RefOff theReferrer)
        {
            InitializeComponent();
            referrer = theReferrer;

            //Set the database connection string
            connection = @"Data Source=RefOff_DB.sdf";
        }

        //This method is called when the 'Save' button is clicked
        private void refProfSaveBtn_Click(object sender, EventArgs e)
        {        
            //Connect to the database
            connectToDatabase(connection);

            //Create and execute SQL command to update referee details in database
            string referee_update_command = @"UPDATE referee SET forename = @forename, surname = @surname, address = @address, NPD_number = @npd, FAMOA_number = @famoa WHERE referee_id=1;";
            using (SqlCeCommand referee_update = new SqlCeCommand(referee_update_command, conn))
            {
                //Firstname
                referee_update.Parameters.AddWithValue("@forename", refProfFirstnameTxt.Text);
                //Surname
                referee_update.Parameters.AddWithValue("@surname", refProfSurnameTxt.Text);
                //Address
                referee_update.Parameters.AddWithValue("@address", refProfAddressTxt.Text);
                //NPD Number
                referee_update.Parameters.AddWithValue("@npd", refProfNPDTxt.Text);
                //FAMOA Number
                referee_update.Parameters.AddWithValue("@famoa", refProfFAMOATxt.Text);
                referee_update.ExecuteNonQuery();
            }

            //Disconnect from database
            closeDatabaseConnection();

            //Hide this form and display the main RefOff screen
            referrer.Enabled = true;
            this.Visible = false;

            //Update the text boxes on the form to reflect the new
            //referee details entered by the user for when the form
            //is opened again in future
            referrer.getRefereeDetails(connection);
        }

        //This method is called when the 'Close' button is clicked
        private void refOffCloseBtn_Click(object sender, EventArgs e)
        {
            //Hide this form and display the main RefOff screen
            referrer.Enabled = true;
            this.Visible = false;

            //Cancel any changes made by the user
            referrer.getRefereeDetails(connection);
        }

        //Method used to connect to the database
        private void connectToDatabase(String connection_string)
        {
            // Retrieve the connection string
            String conString = connection_string;

            // Open the connection using the connection string
            conn = new SqlCeConnection(conString);
            conn.Open();
        }

        //Method to disconnect from the database
        private void closeDatabaseConnection()
        {
            //Close the database connection
            conn.Close();
        }        
    }
}
